package com.nebula.wxlogin.controller.wx;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * @Author: llq
 * @Date: 2019/9/25
 * 无用代码
 */
@RestController
@RequestMapping("/wx")
public class WxLogin {

    @Value("wx.app.appid")
    String appId;

    @Value("wx.app.redirect_url")
    String redirect_url;

    @RequestMapping("/")
    public void login(HttpServletResponse response) throws Exception {
        //这里是回调的url
        String redirect_uri = URLEncoder.encode("http://回调页面的路径", "UTF-8");
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=APPID" +
                "&redirect_uri=REDIRECT_URI"+
        "&response_type=code" +
                "&scope=SCOPE" +
                "&state=123#wechat_redirect";
        response.sendRedirect(url.replace("APPID",appId).replace("REDIRECT_URL",redirect_url).replace("SCOPE","snsapi_userinfo"));
    }






//    @RequestMapping("/index")
//    public void index(String code) {
//        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
//        //开始请求url地址，第一次请求通过code获取openid与access_token
//        UrlConnUtils.get(url.replace("APPID", "你的APPID").replace("SECRET", "你的SECRET")
//                .replace("CODE", code), new GetDataUrlConnListener() {
//            @Override
//            public void onSuccess(HttpURLConnection connection) throws IOException {
//                String data = MyUtils.inputStreamToString(connection.getInputStream());
//                JSONObject jsonObject = JSONObject.parseObject(data);
//                if (jsonObject.getString("openid") != null) {
//                    //拉取用户信息
//                    String openid = jsonObject.getString("openid");
//                    String access_token = jsonObject.getString("access_token");
//                    String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
//                    //第二次请求，用openid与access_token获取用户的信息
//                    UrlConnUtils.get(url.replace("OPENID", openid).replace("ACCESS_TOKEN", access_token), new GetDataUrlConnListener() {
//                        @Override
//                        public void onSuccess(HttpURLConnection connection) throws IOException {
//                            String data = MyUtils.inputStreamToString(connection.getInputStream());
//                            System.out.println(data);//输出微信返回的用户信息
//                        }
//                    });
//                };
//            }
//        });
//    }
}